﻿using System.Collections;
using com.winter.manager;
using UnityEngine;
using UnityEngine.UI;

namespace com.winter.view._02_game
{
    public class WinPanel : MonoBehaviour
    {
        [Header("胜利界面的小星星")] public Image[] stars;

        [Header("胜利时的烟花特效")] public new ParticleSystem particleSystem;

        public GameObject btnMenu;
        private static readonly int Win = Animator.StringToHash("win");

        private void Start()
        {
            btnMenu = transform.Find("btn_menu").gameObject;
            initView();
        }

        private void initView()
        {
            // 隐藏面板
            gameObject.SetActive(true);
            // 隐藏小星星
            foreach (Image star in stars)
            {
                star.gameObject.SetActive(false);
            }

            btnMenu.SetActive(false);

            // 关闭烟花粒子
            particleSystem.Stop(true);
        }

        /// <summary>
        /// 动画系统会来调用这个显示星星的方法
        /// </summary>
        public void show()
        {
            particleSystem.Play();
            StartCoroutine(nameof(showStarAni));
        }


        public void win()
        {
            transform.GetComponent<Animator>().SetBool(Win, true);
            btnMenu.SetActive(true);
        }

        IEnumerator showStarAni()
        {
            for (int i = 0; i < GameManager.getInstance().birds.Count + 1; i++)
            {
                yield return new WaitForSeconds(0.4f);
                stars[i].gameObject.SetActive(true);
            }
        }

        private void OnDestroy()
        {
            Destroy(particleSystem.gameObject);
        }
    }
}